<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use app\admin\model\Goods as GoodsModel;//模型
class Goods extends Permissions
{
    /**
     * 列表
     * @return [type] [description]
     */
    public function index()
    {
        //实例化模型
        $model = new GoodsModel();

        $post = $this->request->param();
        if (isset($post['goods_name']) and !empty($post['goods_name'])) {
            $where['goods_name'] = ['like', '%' . $post['goods_name'] . '%'];
        }

        $admin = empty($where) ? $model->order('id desc')->paginate(20) : $model->where($where)->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        $this->assign('admin', $admin);
        $this->assign('post', $post);
        return $this->fetch();
    }


    /**
     * 的添加及修改
     * @return [type] [description]
     */
    public function publish()
    {
        //获取id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new GoodsModel();
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['goods_name', 'require', '名称不能为空|商户名格式只能是字母、数组、——或_'],
                    ['goods_desc', 'require', '描述不能为空'],
                    ['goods_image', 'require', '商品图片不能为空'],
                    ['goods_images', 'require', '商品图片不能为空'],
                    ['goods_price', 'require', '商品售价不能为空'],
                    ['line_price', 'require', '划线价格不能为空'],
                    ['category_id', 'require', '分类id不能为空'],
                    ['content', 'require', '内容不能为空'],
                    ['sales_initial', 'require', '初始化销量不能为空'],
                    ['sales_actual', 'require', '实际销量不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证商户名是否存在

                if (isset($post['goods_images']) and !empty($post['goods_images'])) {
                    $post['goods_images'] = json_encode($post['goods_images']);
                }

                // 更新商品基本信息
                $result = Db::name('goods')->where('id', $id)->update($post);
                if (!$result) {
                    return $this->error('修改失败');
                }
                // 添加商品规格信息
                addlog($model->id);//写入日志
                return $this->success('修改成功', 'admin/goods/index');
//                }
            } else {
                $category = Db::name('category')->where('status', 1)->select();
                //非提交操作
                $info = $model->where('id', $id)->find();
                $info['goods_images'] = json_decode($info['goods_images']);
                $this->assign('info', $info);
                $this->assign('category', $category);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['goods_name', 'require', '名称不能为空|商户名格式只能是字母、数组、——或_'],
                    ['goods_desc', 'require', '描述不能为空'],
                    ['goods_image', 'require', '商品图片不能为空'],
                    ['goods_images', 'require', '商品图片不能为空'],
                    ['goods_price', 'require', '商品售价不能为空'],
                    ['line_price', 'require', '划线价格不能为空'],
                    ['category_id', 'require', '分类id不能为空'],
                    ['content', 'require', '内容不能为空'],
                    ['sales_initial', 'require', '初始化销量不能为空'],
                    ['sales_actual', 'require', '实际销量不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if (isset($post['goods_images']) and !empty($post['goods_images'])) {
                    $post['goods_images'] = json_encode($post['goods_images']);
                }

                $goods = $model->allowField(true)->insert($post);
                if (!$goods) {
                    return $this->error('添加失败');
                }

                    addlog($model->id);//写入日志
                return $this->success('添加成功', 'admin/goods/index');
            } else {
                //非提交操作
                $category = Db::name('category')->where('status', 1)->select();
                $this->assign('category', $category);
                return $this->fetch();
            }
        }
    }


    /**
     * 删除
     * @return [type] [description]
     */
    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if (false == Db::name('goods')->where('id', $id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功', 'admin/goods/index');
            }
        }
    }

    public function status()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (false == Db::name('goods')->where('id', $post['id'])->update(['status' => $post['status']])) {
                return $this->error('设置失败');
            } else {
                addlog($post['id']);//写入日志
                return $this->success('设置成功', 'admin/goods/index');
            }
        }
    }
}
